package com.ry.est.service.impl;

import com.ry.est.constants.Constants;
import com.ry.est.dao.ClassifyDao;
import com.ry.est.dto.BrandDTO;
import com.ry.est.dto.ClassifyDTO;
import com.ry.est.dto.ModelDTO;
import com.ry.est.entity.Classify;
import com.ry.est.entity.Model;
import com.ry.est.service.BrandService;
import com.ry.est.service.ClassifyService;
import com.ry.est.service.ModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 类别业务接口
 */

@Service
public class ClassifyServiceImpl implements ClassifyService {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private ClassifyDao classifyDao;

    @Autowired
    private BrandClassifyRelationServiceImpl brandClassifyRelationService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private ModelService modelService;

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Override
    public List<ClassifyDTO> listClassifyTree() {
        //先查询缓存
        List<ClassifyDTO> classifyDTOS = (List<ClassifyDTO>) redisTemplate
                .opsForValue()
                .get(Constants.Redis.KEY_HOME_TREE);

        if (!ObjectUtils.isEmpty(classifyDTOS)){
            return classifyDTOS;
        }

        //查询类别数据
        classifyDTOS = listClassifyBy(Constants.Status.SALE,Constants.EST_NOT);

        for (int i = 0; i < classifyDTOS.size(); i++) {
            ClassifyDTO classifyDTO = classifyDTOS.get(i);
            Long classifyId = classifyDTO.getId();
            //根据类别查询品牌id
            List<Long> brandIds = brandClassifyRelationService.listBrandIdsByClassifyId(classifyId);
            //根据品牌id查询品牌
            List<BrandDTO> brandDTOS = brandService.listBrandByIdsAndStatus(brandIds, Constants.Status.SALE);

            classifyDTO.setBrandList(brandDTOS);

            //根据类别和品牌查询商品
            for (int j = 0; j < brandDTOS.size(); j++) {
                BrandDTO brandDTO = brandDTOS.get(j);
                Long brandId = brandDTO.getId();

                Model model = new Model();
                model.setClassifyId(classifyId);
                model.setBrandId(brandId);
                model.setStatus(Constants.Status.SALE);
                model.setIsDelete(Constants.EST_NOT);

                List<ModelDTO> modelDTOS = modelService.listBy(model, 1, 5);
                brandDTO.setModelList(modelDTOS);
            }
        }

        //保存到缓存中
        redisTemplate
                .opsForValue()
                .set(Constants.Redis.KEY_HOME_TREE,classifyDTOS,2, TimeUnit.HOURS);


        return classifyDTOS;
    }

    private List<ClassifyDTO> listClassifyBy(Integer sale, Integer isDelete) {
        Classify record = new Classify();
        record.setStatus(sale);
        record.setIsDelete(isDelete);
        List<Classify> classifies = classifyDao.listBy(record);
        List<ClassifyDTO> classifyDTOS = classifies.stream().map(entity -> {
            ClassifyDTO classifyDTO = new ClassifyDTO();
            classifyDTO.setId(entity.getId());
            classifyDTO.setClassifyName(entity.getClassifyName());
            classifyDTO.setIcon(entity.getIcon());
            classifyDTO.setBigIcon(entity.getBigIcon());

            return classifyDTO;
        }).collect(Collectors.toList());


        return classifyDTOS;
    }
}
